package fms.models;
 
import java.util.List;
 
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import fms.classes.Organization;
 
 
public class OrganizationDAO {
 
    private SqlSessionFactory sqlSessionFactory;
 
    public OrganizationDAO(){
    	try{
    		sqlSessionFactory = MyBatisSqlConnection.getSqlSessionFactory();
    	}catch(Exception e) {
    		System.out.println("failed to get sqlSessionFactory");
    	}
    }
 
    /**Returns the list of all Organization instances from the database*/
    @SuppressWarnings("unchecked")
    public List<Organization> selectAll(){

        SqlSession session = sqlSessionFactory.openSession();
 
        try {
            List<Organization> list = session.selectList("Organization.getAll");
            return list;
        } finally {
            session.close();
        }
    }
 
    /**Returns a Organization instance from the database*/
    public Organization selectById(int org_id){
 
        SqlSession session = sqlSessionFactory.openSession();
 
        try {
            Organization org = (Organization) session.selectOne("Organization.getById",org_id);
            return org;
        } finally {
            session.close();
        }
    }
 
    /**Updates an instance of Organization in the database*/
    public void update(Organization org){
 
        SqlSession session = sqlSessionFactory.openSession();
 
        try {
            session.update("Organization.update", org);
            session.commit();
        } finally {
            session.close();
        }
    }
 
    /** Insert an instance of Organization into the database*/
    public void insert(Organization org){
 
        SqlSession session = sqlSessionFactory.openSession();
 
        try {
            session.insert("Organization.insert", org);
            session.commit();
        } finally {
            session.close();
        }
    }
 
    /** Delete an instance of Organization from the database*/
    public void delete(int org_id){
 
        SqlSession session = sqlSessionFactory.openSession();
 
        try {
            session.delete("Organization.deleteById", org_id);
            session.commit();
        } finally {
            session.close();
        }
    }
}